package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 王磊
 * @date 2022/5/11
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService bookService;

    /**
     * 查询所有地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = bookService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        bookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     * 设置是否为默认收货地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> isDefault(@RequestBody AddressBook addressBook) {
        List<AddressBook> list = bookService.list();
        list = list.stream().map((itam) -> {
            itam.setIsDefault(0);
            return itam;
        }).collect(Collectors.toList());
        bookService.updateBatchById(list);
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getId,addressBook.getId());
        addressBook.setIsDefault(1);
        bookService.update(addressBook,queryWrapper);
        return R.success("修改成功");
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        List<AddressBook> list = bookService.list();
        for (AddressBook addressBook : list) {
            if (addressBook.getIsDefault() == 1) {
                return R.success(addressBook);
            }
        }
        return R.error("没有默认地址");
    }


    /**
     * 根据id查询地址,给浏览器回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> update(@PathVariable Long id) {
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getId, id);
        AddressBook book = bookService.getOne(lambdaQueryWrapper);
        return R.success(book);
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        bookService.updateById(addressBook);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids) {
        bookService.removeById(ids);
        return R.success("删除成功");
    }
}
